package solution1.demo_s;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class TcpClient {

    private Socket socket;

    public TcpClient(String ip, int port) throws IOException {
        this.socket = new Socket(ip, port);
    }

    public void start() {
        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream()) {
            Scanner clientInput = new Scanner(System.in);
            Scanner scanner = new Scanner(inputStream);
            PrintWriter printWriter = new PrintWriter(outputStream);
            while(true) {
                System.out.print("-> ");
                //1.构造请求
                String request = clientInput.next();
                //2.发送请求
                printWriter.println(request);
                printWriter.flush();
                //3.接收响应
                String response = scanner.next();
                System.out.println("响应: " + response);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}

class ClientMain {
    public static void main(String[] args) throws IOException {
        TcpClient tcpClient = new TcpClient("127.0.0.1", 9090);
        tcpClient.start();
    }
}